<template>
  <div class="login-page">
    <div class="left-layout flex flex-v">
      <div class="logo-box flex align-center">
        <img src="@/assets/images/logo-text.png" alt="" class="logo">
      </div>
      <div class="wrapper f1 flex flex-v">
        <div class="brand">
          <p>企业级AI</p>
          <p>一站式智能创意生产与管理平台</p>
        </div>
        <el-carousel style="margin-top: 50px">
          <el-carousel-item
            v-for="item in bannerList"
            :key="item.id"
            class="img"
          >
            <el-image
              crossorigin="anonymous"
              class="whole"
              :src="item.picUrl"
              fit="cover"
            >
              <template slot="error">
                <img src="@/assets/images/login-img.png" alt="" class="whole">
              </template>
            </el-image>
          </el-carousel-item>
        </el-carousel>
      </div>
    </div>
    <div class="right-layout rel flex flex-v align-ceter flex-c">
      <div v-if="type == 'pass' || type == 'code'">
        <div class="tab-list flex align-center" style="height: 50px">
          <div
            class="tab-item"
            :class="{ active: type == 'pass' }"
            @click="type = 'pass'"
          >
            密码登录
          </div>
          <div
            class="tab-item"
            :class="{ active: type == 'code' }"
            @click="type = 'code'"
          >
            验证码登录
          </div>
        </div>
        <div class="form-box">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            auto-complete="off"
            label-position="left"
          >
            <el-form-item prop="username">
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="请输入手机号"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="off"
                style="height: 60px"
              />
            </el-form-item>

            <div v-if="type == 'pass'">
              <el-form-item prop="password">
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  placeholder="请输入密码"
                  :type="passwordType"
                  name="password"
                  tabindex="2"
                  auto-complete="off"
                  style="height: 60px"
                  @keyup.enter.native="handleLogin"
                />
              </el-form-item>
              <div class="flex-b align-center" style="height: 30px">
                <a
                  href="javascript:;"
                  class="forget link"
                  @click="toRegister"
                >马上注册</a>
                <span class="forget" @click="toforget">忘记密码</span>
              </div>
            </div>

            <el-form-item v-else prop="code">
              <div class="flex-b align-center rel">
                <el-input
                  ref="code"
                  v-model="loginForm.code"
                  placeholder="请输入验证码"
                  name="code"
                  tabindex="2"
                  style="height: 60px"
                  auto-complete="off"
                />
                <a href="javascript:;" class="link get-code" @click="getCode">{{
                  codeText
                }}</a>
              </div>
              <div style="height: 30px" />
            </el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              class="form-btn"
              @click.native.prevent="handleLogin"
            >登 录</el-button>

            <div class="tips">
              <el-checkbox
                v-model="checked"
              >我已阅读并同意
                <a class="link" href="javascript:;">《服务协议》</a>和
                <a class="link" href="javascript:;">《隐私协议》</a>
              </el-checkbox>
            </div>
            <div class="icon-box">
              <div class="icon-item">
                <img src="@/assets/images/wx.png" alt="" class="icon">
                <span>微信登录</span>
              </div>
            </div>
            <div class="divider">
              <el-divider
                content-position="center"
              >首次登录自动注册</el-divider>
            </div>
          </el-form>
        </div>
      </div>
      <div v-else>
        <div class="brand">
          {{ type == "setPass" ? "设置登录密码" : "重置登录密码" }}
        </div>
        <div class="form-box">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            auto-complete="off"
            label-position="left"
          >
            <el-form-item prop="username">
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="请输入手机号"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="off"
              />
            </el-form-item>

            <el-form-item prop="code">
              <div class="flex-b align-center rel">
                <el-input
                  ref="code"
                  v-model="loginForm.code"
                  placeholder="请输入验证码"
                  name="code"
                  tabindex="2"
                  auto-complete="off"
                />
                <a href="javascript:;" class="link get-code" @click="getCode">{{
                  codeText
                }}</a>
              </div>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                ref="password"
                v-model="loginForm.password"
                type="password"
                placeholder="请设置密码"
                name="password"
                tabindex="1"
                auto-complete="off"
              />
            </el-form-item>
            <el-form-item prop="repassword">
              <el-input
                ref="repassword"
                v-model="loginForm.repassword"
                type="password"
                placeholder="再次输入密码"
                name="repassword"
                tabindex="1"
                auto-complete="off"
              />
            </el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              class="form-btn"
              @click.native.prevent="handleLogin"
            >登 录</el-button>

            <div class="tips">
              <el-checkbox
                v-model="checked"
              >我已阅读并同意
                <a class="link" href="javascript:;">《服务协议》</a>和
                <a class="link" href="javascript:;">《隐私协议》</a>
              </el-checkbox>
            </div>
            <div class="icon-box">
              <div class="icon-item">
                <img src="@/assets/images/wx.png" alt="" class="icon">
                <span>微信登录</span>
              </div>
            </div>
            <div class="divider">
              <el-divider
                content-position="center"
              >首次登录自动注册</el-divider>
            </div>
          </el-form>
        </div>
      </div>
      <div class="kf-icon">
        <img src="@/assets/images/kefu.png" alt="">
      </div>
      <div class="abs copyright">
        <div>Copyright © 2023 All Rights Reserved 璟暮科技（济宁）</div>
        <div>有限公司鲁ICP备2021036948号-1</div>
        <div>鲁公安备222222222222</div>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
::v-deep .form-btn.el-button {
  span {
    font-size: 20px;
  }
}
.login-page {
  height: 100%;
  display: grid;
  grid-template-columns: 50% 1fr;
  .right-layout {
    background-color: #fff;
    box-sizing: border-box;
    // padding: 0 30% 20px;
    width: 450px;
    margin: 0 auto;
    .tab-list {
      align-items: flex-end;
      .tab-item {
        font-size: 20px;
        font-weight: 400;
        line-height: 36px;
        color: rgba(128, 128, 128, 1);
        position: relative;
        margin-right: 26px;
        padding-bottom: 5px;
        cursor: pointer;
        transition: all 0.1s linear;
        &::after {
          width: 0;
          content: "";
          position: absolute;
          width: 100%;
          height: 1px;
          bottom: 0;
          left: 0;
          background-color: #fff;
        }
      }
      .tab-item.active {
        font-size: 26px;
        font-weight: 700;
        line-height: 43.44px;
        color: rgba(0, 0, 0, 1);
        padding-bottom: 0;
        &::after {
          width: 100%;
          background-color: rgba(0, 119, 255, 0.94);
        }
      }
    }
    .get-code {
      white-space: nowrap;
      position: absolute;
      right: 20px;
    }
    .brand {
      font-size: 20px;
      font-weight: 700;
      letter-spacing: 0px;
      line-height: 43.44px;
      color: rgba(0, 0, 0, 1);
      text-align: center;
    }
    .forget {
      font-size: 14px;
      font-weight: 400;
      color: rgba(166, 166, 166, 1);
      position: relative;
      cursor: pointer;
      height: 30px;
      &:hover {
        color: #000;
      }
    }
    .forget.link {
      color: rgba(0, 119, 255, 0.94);
    }
    .form-box {
      margin-top: 0.55rem;
    }
    .form-btn {
      margin-top: 26px;
      width: 100%;
      margin-bottom: 28px;
      height: 60px;
      border-radius: 15px;
      overflow: hidden;
    }
    .icon-box {
      margin-top: 44px;
      .icon-item {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        &:hover {
          transform: scale(1.03);
          transition: all 0.1s linear;
        }
        .icon {
          width: 45px;
          height: 45px;
          margin-bottom: 10px;
        }
        span {
          font-size: 20px;
          font-weight: 500;
          color: rgba(0, 0, 0, 1);
        }
      }
    }
    .divider {
      margin-top: 38px;
    }
    .copyright {
      bottom: 20px;
      left: 0;
      right: 0;
      margin-left: auto;
      margin-right: auto;
      font-size: 14px;
      font-weight: 400;
      line-height: 22px;
      color: rgba(166, 166, 166, 1);
      text-align: center;
    }
  }
  .kf-icon {
    cursor: pointer;
    position: fixed;
    right: 30px;
    bottom: 120px;
    z-index: 1;
    width: 50px;
    height: 50px;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    &:hover {
      box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.45);
    }
    img {
      width: 30px;
      height: 30px;
    }
  }
  .left-layout {
    background-color: rgba(0, 119, 255, 0.94);
    box-sizing: border-box;
    padding: 30px 50px;
    .logo-box {
      .logo {
        width: 184px;
        height: 60px;
      }
      font-size: 20px;
      font-weight: 600;
      color: rgba(255, 255, 255, 1);
    }
    .wrapper {
      box-sizing: border-box;
      // padding: 130px 120px 0;
      padding-top: 80px;
      margin-left: 120px;
      width: 500px;
      margin: 0 auto;
      .brand {
        font-size: 20px;
        font-weight: 700;
        line-height: 57.92px;
        color: rgba(255, 255, 255, 1);
      }
      .img {
        width: 100%;
        border-radius: 15px;
        overflow: hidden;
      }
    }
  }
}
</style>
<script>
import { validUsername } from '@/utils/validate'
import { getCode, register, forgetPass, getLoginBanner } from '@/api/user'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('手机号错误'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (!value || value.length < 4) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }
    const validateRePassword = (rule, value, callback) => {
      if (value !== this.loginForm.password) {
        callback(new Error('两次密码需保持一致'))
      } else {
        callback()
      }
    }
    return {
      bannerList: [],
      type: 'pass', // 密码登录pass，验证码登录code，设置密码setPass，重置密码resetPass
      checked: true,
      codeText: '获取验证码',
      loginForm: {
        username: '',
        password: '',
        code: '',
        repassword: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        code: [{ required: true, trigger: 'blur', validator: validateCode }],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        repassword: [
          { required: true, trigger: 'blur', validator: validateRePassword }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      waitTime: 60,
      timer: null
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getBanner()
  },
  methods: {
    async getBanner() {
      const res = await getLoginBanner()
      if (res.success) {
        this.bannerList = res.result
      } else {
        this.$message.error(res.message)
      }
    },
    toRegister() {
      this.type = 'setPass'
    },
    async getCode() {
      console.log(this.waitTime)
      if (this.waitTime > 0 && this.waitTime !== 60) {
        return
      } else {
        this.codeText = `获取验证码`
        clearInterval(this.timer)
      }
      if (!this.loginForm.username) {
        this.$message.warning('请输入手机号')
        return
      }
      const res = await getCode({
        mobile: this.loginForm.username
      })
      if (res.success) {
        this.$message.success('发送成功')
        this.timer = setInterval(() => {
          this.waitTime -= 1
          this.codeText = `${this.waitTime}秒后重新获取`
          if (this.waitTime === 1) {
            this.codeText = `获取验证码`
            this.waitTime = 60
            clearInterval(this.timer)
          }
        }, 1000)
      }
    },
    toforget() {
      this.type = 'forgetPass'
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {
          console.log(this.type)
          this.loading = true
          // 验证码登录
          if (this.type === 'code') {
            const res = await this.$store.dispatch('user/loginByCode', {
              mobile: this.loginForm.username,
              captcha: this.loginForm.code,
              invite: ''
            })
            if (res.success) {
              this.$message.success('登录成功')
              this.$router.push({ path: this.redirect || '/' })
            } else {
              this.$message.error(res.message)
              this.loading = false
            }
            this.loading = false
          }
          // 密码登录
          if (this.type === 'pass') {
            const res = await this.$store.dispatch('user/loginByPass', {
              password: this.loginForm.password,
              username: this.loginForm.username
            })
            if (res.success) {
              this.$message.success('登录成功')
              this.$router.push({ path: this.redirect || '/' })
            } else {
              this.$message.error(res.message)
              this.loading = false
            }
            this.loading = false
          }
          // 注册
          if (this.type === 'setPass') {
            const res = await register({
              phone: this.loginForm.username,
              password: this.loginForm.password,
              code: this.loginForm.code,
              invite: ''
            })
            if (res.success) {
              this.type = 'pass'
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.message)
              this.loading = false
            }
            this.loading = false
          }
          // 忘记密码
          if (this.type === 'forgetPass') {
            const res = await forgetPass({
              phone: this.loginForm.username,
              password: this.loginForm.password,
              code: this.loginForm.code
            })
            if (res.success) {
              this.$message.success('修改成功')
              this.loginForm.username = ''
              this.loginForm.password = ''
              this.loginForm.code = ''
              this.type = 'pass'
            } else {
              this.$message.error(res.message)
              this.loading = false
            }
            this.loading = false
          }
        }
      })
    }
  }
}
</script>
